package sort;

import java.util.ArrayDeque;
import java.util.Deque;

public class Sort {

    /**
     * 插入排序
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     */
    public static void insertSort(int[] arr) {
        for (int i = 0; i < arr.length; ++i) {
            int temp = arr[i];
            int j = i - 1;
            for (; j >= 0; --j) {
                if (arr[j] > temp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = temp;
        }
    }

    /**
     * 冒泡排序
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     */
    public static void bubbleSort1(int[] arr) {
        for (int i = 0; i < arr.length - 1; ++i) {
            for (int j = i + 1; j < arr.length; ++j) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    public static void bubbleSort2(int[] arr) {
        for (int i = 0; i < arr.length - 1; ++i) {
            boolean flag = true;
            for (int j = 0; j < arr.length - i - 1; ++j) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }
    }

    /**
     * 希尔排序
     * 时间复杂度：不定
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     */
    public static void shellSort(int[] arr) {
        int gap = arr.length;
        while (gap > 1) {
            gap = gap / 2;
            shell(arr, gap);
        }
    }

    private static void shell(int[] arr, int gap) {
        for (int i = gap; i < arr.length; ++i) {
            int temp = arr[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (arr[j] > temp) {
                    arr[j + gap] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + gap] = temp;
        }
    }

    /**
     * 选择排序
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     */
    public static void selectSort1(int[] arr) {
        for (int i = 0; i < arr.length; ++i) {
            int minI = i;
            for (int j = i + 1; j < arr.length; ++j) {
                if (arr[j] < arr[minI]) {
                    minI = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[minI];
            arr[minI] = temp;
        }
    }

    public static void selectSort2(int[] arr) {
        for (int left = 0, right = arr.length - 1; left < right; ++left, --right) {
            int minI = left, maxI = left;
            for (int i = left + 1; i <= right; ++i) {
                if (arr[i] < arr[minI]) {
                    minI = i;
                } else if (arr[i] > arr[maxI]) {
                    maxI = i;
                }
            }
            swap(arr, minI, left);
            if (maxI == left) {
                maxI = minI;
            }
            swap(arr, maxI, right);
        }
    }

    private static void swap(int[] arr, int i, int j) {
        if (i != j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    /**
     * 堆排序
     * 时间复杂度：O(nlogn)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     */
    public static void heapsort(int[] arr) {
        createHeap(arr);
        int end = arr.length - 1;
        while (end > 0) {
            swap(arr, 0, end);
            shiftDown(arr, 0, end);
            end--;
        }
    }

    private static void createHeap(int[] arr) {
        for (int i = (arr.length - 1 - 1) / 2; i >= 0; --i) {
            shiftDown(arr, i, arr.length);
        }
    }

    private static void shiftDown(int[] arr, int root, int len) {
        int child = root * 2 + 1;
        while (child < len) {
            if (child + 1 < len && arr[child] < arr[child + 1]) {
                child++;
            }
            if (arr[child] > arr[root]) {
                swap(arr, child, root);
                root = child;
                child = root * 2 + 1;
            } else {
                break;
            }
        }
    }

    /**
     * 快速排序
     * 时间复杂度：O(nlogn)
     * 空间复杂度：O(logn)
     * 稳定性：不稳定
     */

    // 第一种快速排序
    public static void quickSort1(int[] arr) {
        quickSort1(arr, 0, arr.length - 1);
    }

    private static void quickSort1(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int base = partition1(arr, left, right);
        quickSort1(arr, left, base - 1);
        quickSort1(arr, base + 1, right);
    }

    private static int partition1(int[] arr, int left, int right) {
        int base = arr[left];
        int temp = left;
        while (left < right) {
            while (left < right && arr[right] >= base) {
                right--;
            }
            while (left < right && arr[left] <= base) {
                left++;
            }
            swap(arr, left, right);
        }
        swap(arr, left, temp);
        return left;
    }

    private static int partition2(int[] arr, int left, int right) {
        int base = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= base) {
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= base) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = base;
        return left;
    }

    private static int partition3(int[] arr, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while (cur <= right) {
            if (arr[cur] < arr[left] && arr[++prev] != arr[cur]) {
                swap(arr, cur, prev);
            }
            cur++;
        }
        swap(arr, left, prev);
        return prev;
    }

    // 第二种快速排序，优化版
    public static void quickSort2(int[] arr) {
        quickSort2(arr, 0, arr.length - 1);
    }

    private static void quickSort2(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }

        if (right - left < 7) {
            insertSort(arr, left, right);
            return;
        }
        int mid = getMiddleNum(arr, left, right);
        swap(arr, left, mid);

        int base = partition1(arr, left, right);
        quickSort2(arr, left, base - 1);
        quickSort2(arr, base + 1, right);

    }

    // 插入排序快排特供版
    private static void insertSort(int[] arr, int left, int right) {
        for (int i = left; i <= right; ++i) {
            int temp = arr[i];
            int j = i - 1;
            for (; j >= 0; --j) {
                if (arr[j] > temp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = temp;
        }
    }

    private static int getMiddleNum(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        int min = arr[left];
        if (arr[mid] < min) {
            min = arr[mid];
        }
        if (arr[right] < min) {
            min = arr[right];
        }
        int max = arr[left];
        if (arr[mid] > max) {
            max = arr[mid];
        }
        if (arr[right] > max) {
            max = arr[right];
        }
        return arr[left] + arr[right] + arr[mid] - min - max;
    }

    // 非递归的快速排序
    public static void quickSortNor(int[] arr) {
        quickSortNor(arr, 0, arr.length - 1);
    }

    private static void quickSortNor(int[] arr, int left, int right) {
        Deque<Integer> s = new ArrayDeque<>();
        int pivot = partition2(arr, left, right);
        if (pivot > left + 1) {
            s.push(left);
            s.push(pivot - 1);
        }
        if (pivot < right - 1) {
            s.push(pivot + 1);
            s.push(right);
        }

        while (!s.isEmpty()) {
            right = s.pop();
            left = s.pop();
            pivot = partition2(arr, left, right);
            if (pivot > left + 1) {
                s.push(left);
                s.push(pivot - 1);
            }
            if (pivot < right - 1) {
                s.push(pivot + 1);
                s.push(right);
            }
        }
    }

    /**
     * 归并排序
     * 时间复杂度：O(nlogn)
     * 空间复杂度：O(n)
     * 稳定性：稳定
     */
    public static void mergeSort(int[] arr) {
        mergeSort(arr, 0, arr.length - 1);
    }

    private static void mergeSort(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeSort(arr, left, mid);
        mergeSort(arr, mid + 1, right);
        merge(arr, left, mid, right);
    }

    private static void merge(int[] arr, int left, int mid, int right) {
        int l1 = left;
        int l2 = mid + 1;
        int k = 0;
        int[] temp = new int[right - left + 1];
        while (l1 <= mid && l2 <= right) {
            if (arr[l1] <= arr[l2]) {
                temp[k++] = arr[l1++];
            } else {
                temp[k++] = arr[l2++];
            }
        }

        while (l1 <= mid) {
            temp[k++] = arr[l1++];
        }

        while (l2 <= right) {
            temp[k++] = arr[l2++];
        }

        for (int i = 0; i < temp.length; ++i) {
            arr[left + i] = temp[i];
        }
    }

    // 归并排序非递归
    public static void mergeSortNor(int[] arr) {
        int gap = 1;
        while (gap < arr.length) {
            for (int left = 0; left < arr.length; left += gap * 2) {
                int mid = Math.min(left + gap - 1, arr.length - 1);
                int right = Math.min(mid + gap, arr.length - 1);
                merge(arr, left, mid, right);
            }
            gap *= 2;
        }
    }

    /**
     * 计数排序
     * 时间复杂度：O(range = max - min)
     * 空间复杂度：O(k)
     * 稳定性：稳定
     */
    public static void countSort(int[] arr) {
        int max = arr[0];
        int min = arr[0];
        for (int i = 1; i < arr.length; ++i) {
            if (arr[i] > max) {
                max = arr[i];
            }
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        int len = max - min + 1;
        int[] count = new int[len];
        for (int i : arr) {
            count[i - min]++;
        }
        for (int i = 0, k = 0; i < count.length; ++i) {
            while (count[i]-- > 0) {
                arr[k++] = i + min;
            }
        }
    }

    public static void show(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; ++i) {
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
        }
        System.out.print("]");
    }
}
